package de.gis;

import java.util.Arrays;

import org.apache.http.client.methods.HttpGet;
import org.json.JSONArray;

import android.content.SharedPreferences;
import android.util.Log;

public class FetchTagSuggestions extends CouchCommunicator {

	private static final String TAG = "FetchTagSuggestions";
	private String[] suggestions;

	public String[] getSuggestions() {
		return suggestions;
	}

	public FetchTagSuggestions(String dbUrl, SharedPreferences prefs) {
		super(dbUrl, prefs);
		suggestions = new String[] { "default" };
	}

	@Override
	public void doit() throws Exception {
		String urlStr = dbUrl + "_design/butterfly/_list/tags/tags";
		Log.d(TAG, "Getting suggestions from " + urlStr);
		HttpGet request = new HttpGet(urlStr);
		JSONArray ja = getJSonArrayResponse(request);
		String[] result = new String[ja.length()];
		for (int r = 0; r < result.length; r++) {
			result[r] = ja.getString(r);
		}
		// atomic setting, no syncing needed
		suggestions = result;
		Log.d(TAG, "Got suggestions: " + Arrays.toString(suggestions));
	}

}
